package com.share.service.oa.org.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.share.bean.oa.org.Organization;
import com.share.commons.base.query.BaseQuery;
import com.share.commons.base.query.PageModel;
import com.share.dao.oa.org.OrganizationDao;
import com.share.service.oa.org.OrganizationManager;
import com.share.web.formbean.oa.hr.OrganizationForm;

@Service
public class OrganizationManagerImpl implements OrganizationManager {

	@Resource
	private OrganizationDao orgDao ;
	
	public void setOrgDao(OrganizationDao orgDao) {
		this.orgDao = orgDao;
	}

	@Override
	public void deleteOrgManager(String ids) throws Exception {
		this.orgDao.deleteOrgDao(ids.split(",")) ;
	}

	@Override
	public Organization loadByIdOrgManager(String id) throws Exception {
		
		return this.orgDao.loadById(id) ;
	}


	@Override
	public void saveOrgManager(Organization org) throws Exception {
		org.setAddTime(new Date()) ;
		this.orgDao.saveOrgDao(org) ;
	}

	@Override
	public void updateOrgManager(Organization org) throws Exception {
		org.setAddTime(new Date()) ;
		this.orgDao.updateOrgDao(org) ;
	}

	@Override
	public PageModel<Organization> loadScrollDataOrgManager(OrganizationForm orgVo,int firstResult,int maxResult) throws Exception {
		String hql = "select o from Organization o" ;
		
		StringBuffer whereHQL = new StringBuffer() ;
		List<Object> param = new ArrayList<Object>() ;
		
		if(null != orgVo) {
			if(null != orgVo.getOrgCode() && !"".equals(orgVo.getOrgCode())){
				whereHQL.append("o.orgCode=?") ;
				param.add(orgVo.getOrgCode()) ;
			} else if(null != orgVo.getOrgLegalPeople() && !"".equals(orgVo.getOrgLegalPeople())) {
				whereHQL.append("o.orgLegalPeople=?") ;
				param.add(orgVo.getOrgLegalPeople()) ;
			} else if(null != orgVo.getOrgLegalName() && !"".equals(orgVo.getOrgLegalName())) {
				whereHQL.append("o.orgLegalName=?") ;
				param.add(orgVo.getOrgLegalName()) ; 
			} else if(null != orgVo.getEstablishCountry() && !"".equals(orgVo.getEstablishCountry())) {
				whereHQL.append("o.establishCountry=?") ;
				param.add(orgVo.getEstablishCountry()) ;
			} else if(null != orgVo.getEstablishTime() && !"".equals(orgVo.getEstablishTime())) {
				System.out.println("getEstablishTime()"+!"".equals(orgVo.getEstablishTime())+orgVo.getEstablishTime());
				whereHQL.append("o.establishTime=?") ;
				param.add(orgVo.getEstablishTime()) ;
			} else if(null != orgVo.getStartDate() && null != orgVo.getEndDate()) {
				whereHQL.append("o.establishTime>=?").append(BaseQuery.AND).append("o.establishTime<=?") ;
				param.add(orgVo.getStartDate()) ;
				param.add(orgVo.getEndDate()) ;
			} else if(null != orgVo.getKeyword() && !"".equals(orgVo.getKeyword())) {
				whereHQL.append("o.orgLegalName").append(BaseQuery.LIKE).append("?") ;
				param.add("%"+orgVo.getKeyword()+"%") ;
			}
		}
		
		LinkedHashMap<String,String> orderby = new LinkedHashMap<String, String>() ;
		orderby.put("addTime", BaseQuery.DESC) ;
		
		return this.orgDao.seacheOrgDataDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby) ;
	}

	@Override
	public List<Organization> getOrgAll() throws Exception {
		String hql = "select o from Organization o" ;
		
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>() ;
		orderby.put("addTime", BaseQuery.DESC) ;
		return this.orgDao.getOrgAllDao(hql, orderby) ;
	}	

}
